package com.qfedu.sem.test;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.qfedu.sem.po.BugVersion;
import com.qfedu.sem.po.Project;
import com.qfedu.sem.repository.BugVersionRepository;
import com.qfedu.sem.repository.ProjectRepository;

import wo.bsys.po.Dictionary;
import wo.bsys.po.Menu;
import wo.bsys.po.Role;
import wo.bsys.po.User;
import wo.bsys.repository.DictionaryRepository;
import wo.bsys.repository.MenuRepository;
import wo.bsys.repository.RoleRepository;
import wo.bsys.repository.UserRepository;
import wo.common.util.WoUtil;
import wo.department.po.Position;
import wo.department.repository.PositionRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BSysInitializer {
	private Logger LOG = LogManager.getLogger(BSysInitializer.class);

	@Resource
	private MenuRepository menuRepository;

	@Resource
	private DictionaryRepository dictionaryRepository;

	@Resource
	private RoleRepository roleRepository;

	@Resource
	private PositionRepository positionRepository;

	@Resource
	private UserRepository userRepository;

	@Resource
	private ProjectRepository projectRepository;

	@Resource
	private BugVersionRepository bugVersionRepository;

	private final static String PWD = "123456";

	/**
	 * 初始化系统数据
	 */
	@Test
	public void initData() {
		// 系统管理
		// 父菜单
		Menu m = new Menu("esys", "系统管理", "00", "fa-bars", null, null);
		this.menuRepository.save(m);
		// 子菜单
		Menu c1 = new Menu("esys-menu", "菜单", "0010", "fa-gears", "menu/main.jsp", m);
		this.menuRepository.save(c1);
		Menu c2 = new Menu("esys-role", "角色", "0020", "fa-group", "role/main.jsp", m);
		this.menuRepository.save(c2);
		Menu c3 = new Menu("esys-user", "用户", "0030", "fa-user", "user/main.jsp", m);
		this.menuRepository.save(c3);
		Menu c4 = new Menu("esys-dictionary", "数据字典", "0040", "fa-book", "dictionary/main.jsp", m);
		this.menuRepository.save(c4);
		// 角色：系统管理员
		Role admin = new Role();
		admin.setId("admin");
		admin.setName("系统管理员");
		roleRepository.save(admin);
		// 管理员用户
		User userAdmin = new User();
		userAdmin.setId("admin");
		userAdmin.setLoginName("admin");
		userAdmin.setPassword(WoUtil.getMD5(userAdmin.getId(), PWD));
		userAdmin.setCreateTime(new Date());
		userAdmin.setPasswordTime(new Date());
		userAdmin.setRoles(Arrays.asList(admin));
		userRepository.save(userAdmin);

		// 父菜单：故障管理
		Menu bugman = new Menu("bugman", "故障管理", "10", "fa-exclamation-circle", null, null);
		this.menuRepository.save(bugman);
		// 我的信息子菜单
		Menu myInfo = new Menu("bugman-myInfo", "我的信息", "1010", "fa-gears", "staff/my.jsp", bugman);
		this.menuRepository.save(myInfo);
		// 我提交的故障子菜单
		Menu myBug = new Menu("bugman-myBug", "我提交的故障", "1020", "fa-gears", "bug/my.jsp", bugman);
		this.menuRepository.save(myBug);
		// 我修复的故障子菜单
		Menu myBug2 = new Menu("bugman-myBug2", "我修复的故障", "1021", "fa-gears", "bug/my2.jsp", bugman);
		this.menuRepository.save(myBug2);
		// 我测试的故障子菜单
		Menu myBug3 = new Menu("bugman-myBug3", "我测试的故障", "1022", "fa-gears", "bug/my3.jsp", bugman);
		this.menuRepository.save(myBug3);
		// 人员子菜单
		Menu staff = new Menu("bugman-staff", "人员", "1030", "fa-gears", "staff/main.jsp", bugman);
		this.menuRepository.save(staff);
		// 故障子菜单
		Menu bug = new Menu("bugman-bug", "故障", "1040", "fa-gears", "bug/main.jsp", bugman);
		this.menuRepository.save(bug);
		// 故障统计
		Menu bugCharts = new Menu("bugman-bugcharts", "故障统计", "1050", "fa-gears", "bug/charts.jsp", bugman);
		this.menuRepository.save(bugCharts);
		// 项目
		Menu project = new Menu("bugman-project", "项目", "1060", "fa-gears", "project/main.jsp", bugman);
		this.menuRepository.save(project);
		// 版本
		Menu bugVersion = new Menu("bugman-version", "版本", "1070", "fa-gears", "version/main.jsp", bugman);
		this.menuRepository.save(bugVersion);

		// 父菜单：department管理
		Menu department = new Menu("department", "department管理", "20", "fa-exclamation-circle", null, null);
		this.menuRepository.save(department);
		// department子菜单
		Menu departmentDepartment = new Menu("department-department", "部门", "2010", "fa-gears", "department/main.jsp",
				department);
		this.menuRepository.save(departmentDepartment);
		Menu departmentPosition = new Menu("department-position", "职务", "2020", "fa-gears", "position/main.jsp",
				department);
		this.menuRepository.save(departmentPosition);
		Menu departmentStaff = new Menu("department-staff", "人员", "2030", "fa-gears", "staff/main.jsp", department);
		this.menuRepository.save(departmentStaff);

		// 角色：manager
		Role manager = new Role();
		manager.setId("manager");
		manager.setName("管理者");
		manager.setMenus(Arrays.asList(bugman, myInfo, myBug, myBug2, myBug3, staff, bug, bugCharts, department, departmentDepartment, departmentPosition, departmentStaff, project, bugVersion));
		roleRepository.save(manager);
		// 角色：employee
		Role employee = new Role();
		employee.setId("employee");
		employee.setName("员工");
		employee.setMenus(Arrays.asList(bugman, myInfo, myBug, myBug2, myBug3));
		roleRepository.save(employee);

		// 职务
		Position position = new Position("1", "管理者", "管理者");
		positionRepository.save(position);
		position = new Position("2", "员工", "普通员工");
		positionRepository.save(position);

		// 数据字典
		// 职务
		Dictionary d = new Dictionary("1", "position", "1", "管理者", null);
		this.dictionaryRepository.save(d);
		d = new Dictionary("2", "position", "2", "普通员工", null);
		this.dictionaryRepository.save(d);
		d = new Dictionary("3", "gender", "1", "男", null);
		this.dictionaryRepository.save(d);
		d = new Dictionary("4", "gender", "2", "女", null);
		this.dictionaryRepository.save(d);

		d = new Dictionary("5", "bugLevel", "5-CRITICAL", "5-CRITICAL", "死机，数据丢失，主要功能完全丧失，系统挂起等错误");
		this.dictionaryRepository.save(d);
		d = new Dictionary("6", "bugLevel", "2-LOW", "2-LOW", "微小的问题，对功能几乎没有影响");
		this.dictionaryRepository.save(d);
		d = new Dictionary("7", "bugLevel", "4-MAJOR", "4-MAJOR", "主要功能丧失导致严重的问题，或者致命的异常");
		this.dictionaryRepository.save(d);
		d = new Dictionary("8", "bugLevel", "3-MINOR", "3-MINOR", "次要功能丧失，不太严重。如提示信息不准确");
		this.dictionaryRepository.save(d);
		d = new Dictionary("9", "bugLevel", "1-NONE", "1-NONE", "对用户使用无实际影响");
		this.dictionaryRepository.save(d);

		d = new Dictionary("10", "bugRate", "5-INEVITABLE", "5-INEVITABLE",
				"故障出现在每日使用的核心模块或者核心模块的重要功能、登陆首页或者第一次使用就出现;核心常用模块且用户经常使用的简单并发操作出现");
		this.dictionaryRepository.save(d);
		d = new Dictionary("11", "bugRate", "4-COMMON", "4-COMMON",
				"故障出现在每周使用的常用模块，无需对功能模块有特别了解及输入特殊数据;核心常用模块但用户不经常使用的简单并发操作出现");
		this.dictionaryRepository.save(d);
		d = new Dictionary("12", "bugRate", "3-INFREQUENT", "3-INFREQUENT",
				"故障只出现在长时间的功能操作或者输入特殊数据情况；非常用模块用户进行复杂的并发操作出现");
		this.dictionaryRepository.save(d);
		d = new Dictionary("13", "bugRate", "2-OCCASIONAL", "2-OCCASIONAL", "故障只出现在性能限制下，如长时间的压力条件下");
		this.dictionaryRepository.save(d);

		Project proj = new Project("DC", "点餐系统");
		this.projectRepository.save(proj);
		BugVersion version = new BugVersion("DC_v1.00.a", "DC_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("DC_v1.00.b", "DC_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("DC_v1.00.c", "DC_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("DC_v1.00.d", "DC_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("DC_v1.00.e", "DC_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("SHOP", "电商平台");
		this.projectRepository.save(proj);
		version = new BugVersion("SHOP_v1.00.a", "SHOP_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SHOP_v1.00.b", "SHOP_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SHOP_v1.00.c", "SHOP_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SHOP_v1.00.d", "SHOP_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SHOP_v1.00.e", "SHOP_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("JW", "教务系统");
		this.projectRepository.save(proj);
		version = new BugVersion("JW_v1.00.a", "JW_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("JW_v1.00.b", "JW_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("JW_v1.00.c", "JW_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("JW_v1.00.d", "JW_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("JW_v1.00.e", "JW_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("CRM", "CRM系统");
		this.projectRepository.save(proj);
		version = new BugVersion("CRM_v1.00.a", "CRM_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CRM_v1.00.b", "CRM_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CRM_v1.00.c", "CRM_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CRM_v1.00.d", "CRM_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CRM_v1.00.e", "CRM_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("HR", "人事管理系统");
		this.projectRepository.save(proj);
		version = new BugVersion("HR_v1.00.a", "HR_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("HR_v1.00.b", "HR_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("HR_v1.00.c", "HR_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("HR_v1.00.d", "HR_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("HR_v1.00.e", "HR_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("BOOK", "在线书城系统");
		this.projectRepository.save(proj);
		version = new BugVersion("BOOK_v1.00.a", "BOOK_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("BOOK_v1.00.b", "BOOK_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("BOOK_v1.00.c", "BOOK_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("BOOK_v1.00.d", "BOOK_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("BOOK_v1.00.e", "BOOK_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("SCHOOL", "学校二手交易系统");
		this.projectRepository.save(proj);
		version = new BugVersion("SCHOOL_v1.00.a", "SCHOOL_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SCHOOL_v1.00.b", "SCHOOL_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SCHOOL_v1.00.c", "SCHOOL_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SCHOOL_v1.00.d", "SCHOOL_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SCHOOL_v1.00.e", "SCHOOL_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("NEWS", "新闻发布系统");
		this.projectRepository.save(proj);
		version = new BugVersion("NEWS_v1.00.a", "NEWS_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("NEWS_v1.00.b", "NEWS_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("NEWS_v1.00.c", "NEWS_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("NEWS_v1.00.d", "NEWS_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("NEWS_v1.00.e", "NEWS_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("CINEMA", "影院票务系统");
		this.projectRepository.save(proj);
		version = new BugVersion("CINEMA_v1.00.a", "CINEMA_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CINEMA_v1.00.b", "CINEMA_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CINEMA_v1.00.c", "CINEMA_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CINEMA_v1.00.d", "CINEMA_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("CINEMA_v1.00.e", "CINEMA_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("REGISTER", "医院挂号系统");
		this.projectRepository.save(proj);
		version = new BugVersion("REGISTER_v1.00.a", "REGISTER_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("REGISTER_v1.00.b", "REGISTER_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("REGISTER_v1.00.c", "REGISTER_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("REGISTER_v1.00.d", "REGISTER_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("REGISTER_v1.00.e", "REGISTER_v1.00.e", proj);
		this.bugVersionRepository.save(version);

		proj = new Project("SEM", "软件过程管理系统");
		this.projectRepository.save(proj);
		version = new BugVersion("SEM_v1.00.a", "SEM_v1.00.a", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SEM_v1.00.b", "SEM_v1.00.b", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SEM_v1.00.c", "SEM_v1.00.c", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SEM_v1.00.d", "SEM_v1.00.d", proj);
		this.bugVersionRepository.save(version);
		version = new BugVersion("SEM_v1.00.e", "SEM_v1.00.e", proj);
		this.bugVersionRepository.save(version);
	}
}
